import { request } from '@/service/request';

/**
 * 元数据管理API
 */

/** 元数据查询参数 */
export interface MetadataQuery {
  datasourceId?: number;
  databaseName?: string;
  tableName?: string;
  tableType?: string;
  projectId?: number;
  status?: number;
  keyword?: string;
}

/** 元数据VO */
export interface MetadataVO {
  id: number;
  datasourceId: number;
  datasourceName: string;
  databaseName: string;
  tableName: string;
  tableComment: string;
  tableType: string;
  fields: FieldInfo[];
  indexes: IndexInfo[];
  dataSize: number;
  dataSizeFormatted: string;
  rowCount: number;
  lastCollectTime: string;
  projectId: number;
  projectName: string;
  status: number;
  createTime: string;
  updateTime: string;
  remark: string;
}

/** 字段信息 */
export interface FieldInfo {
  fieldName: string;
  fieldType: string;
  fieldComment: string;
  nullable: boolean;
  defaultValue: string;
  primaryKey: boolean;
}

/** 索引信息 */
export interface IndexInfo {
  indexName: string;
  indexType: string;
  columns: string[];
  unique: boolean;
}

/** 数据血缘VO */
export interface DataLineageVO {
  id: number;
  sourceTableId: number;
  sourceTableName: string;
  sourceDatabaseName: string;
  targetTableId: number;
  targetTableName: string;
  targetDatabaseName: string;
  lineageType: string;
  transformLogic: string;
  taskId: number;
  taskName: string;
  taskType: string;
  projectId: number;
  projectName: string;
  status: number;
  createTime: string;
  updateTime: string;
}

/** 影响分析结果 */
export interface ImpactAnalysisDTO {
  tableId: number;
  tableName: string;
  changeType: string;
  upstreamCount: number;
  downstreamCount: number;
  affectedTasks: AffectedTask[];
  affectedApis: AffectedApi[];
  impactLevel: string;
  impactDescription: string;
}

export interface AffectedTask {
  taskId: number;
  taskName: string;
  taskType: string;
  impactType: string;
}

export interface AffectedApi {
  apiId: number;
  apiName: string;
  apiPath: string;
  impactType: string;
}

/** 血缘图数据 */
export interface LineageGraph {
  nodes: LineageNode[];
  edges: LineageEdge[];
}

export interface LineageNode {
  id: number;
  label: string;
  type: string;
}

export interface LineageEdge {
  source: number;
  target: number;
  label: string;
}

/**
 * 查询元数据列表
 */
export function fetchMetadataList(params: MetadataQuery & Common.PaginatingQueryRecord) {
  return request<ApiDataplatform.PageResult<MetadataVO>>({
    url: '/dataplatform/metadata/list',
    method: 'get',
    params
  });
}

/**
 * 查询元数据详情
 */
export function fetchMetadataDetail(id: number) {
  return request<MetadataVO>({
    url: `/dataplatform/metadata/${id}`,
    method: 'get'
  });
}

/**
 * 采集数据源元数据
 */
export function collectMetadata(datasourceId: number) {
  return request<number>({
    url: `/dataplatform/metadata/collect/${datasourceId}`,
    method: 'post'
  });
}

/**
 * 采集指定表的元数据
 */
export function collectTableMetadata(params: {
  datasourceId: number;
  databaseName: string;
  tableName: string;
}) {
  return request<number>({
    url: '/dataplatform/metadata/collect/table',
    method: 'post',
    params
  });
}

/**
 * 搜索元数据
 */
export function searchMetadata(keyword: string, params: Common.PaginatingQueryRecord) {
  return request<ApiDataplatform.PageResult<MetadataVO>>({
    url: '/dataplatform/metadata/search',
    method: 'get',
    params: { keyword, ...params }
  });
}

/**
 * 导出元数据
 */
export function exportMetadata(data: MetadataQuery) {
  return request<MetadataVO[]>({
    url: '/dataplatform/metadata/export',
    method: 'post',
    data
  });
}

/**
 * 导入元数据
 */
export function importMetadata(data: MetadataVO[]) {
  return request<number>({
    url: '/dataplatform/metadata/import',
    method: 'post',
    data
  });
}

/**
 * 删除元数据
 */
export function deleteMetadata(id: number) {
  return request({
    url: `/dataplatform/metadata/${id}`,
    method: 'delete'
  });
}

/**
 * 批量删除元数据
 */
export function batchDeleteMetadata(ids: number[]) {
  return request({
    url: '/dataplatform/metadata/batch',
    method: 'delete',
    data: ids
  });
}

/**
 * 构建数据血缘
 */
export function buildLineage(params: { taskId: number; taskType: string }) {
  return request({
    url: '/dataplatform/metadata/lineage/build',
    method: 'post',
    params
  });
}

/**
 * 查询上游依赖
 */
export function fetchUpstream(tableId: number) {
  return request<DataLineageVO[]>({
    url: `/dataplatform/metadata/lineage/upstream/${tableId}`,
    method: 'get'
  });
}

/**
 * 查询下游影响
 */
export function fetchDownstream(tableId: number) {
  return request<DataLineageVO[]>({
    url: `/dataplatform/metadata/lineage/downstream/${tableId}`,
    method: 'get'
  });
}

/**
 * 影响分析
 */
export function analyzeImpact(params: { tableId: number; changeType: string }) {
  return request<ImpactAnalysisDTO>({
    url: '/dataplatform/metadata/impact/analyze',
    method: 'get',
    params
  });
}

/**
 * 查询血缘关系图
 */
export function fetchLineageGraph(tableId: number, depth?: number) {
  return request<LineageGraph>({
    url: `/dataplatform/metadata/lineage/graph/${tableId}`,
    method: 'get',
    params: { depth }
  });
}
